package com.paireach.szwolaiyun.fragment;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.mode.SignMode;
import com.paireach.szwolaiyun.util.BarCodeUtil;
import com.paireach.szwolaiyun.util.MessageInfoUtil;
import com.paireach.szwolaiyun.util.StringUtil;

/**
 * Created by Administrator on 2016/6/21.
 */
public class QueueFragment extends Fragment{
    private View view;
    private Handler handler;
    private RelativeLayout bottom_bar_code;
    private Bitmap bitmap;
    private ImageView barCodeImg;
    private TextView bar_code_num;
    private BarCodeUtil barCodeUtil;
    private SignMode signMode;

    private TextView message_title;
    private TextView message_deletails;
    private TextView message_deletails2;
    private TextView message_deletails3;
    private TextView prompt;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_queue,null);
        initinitControl();
        initView();
        return view;
    }

    private void initView(){
        if(null!=signMode){
            hideAllView();
            if(signMode.getStatusCode().equals(StringUtil.LINE_UP)){
                //装货工厂外排队
                Log.e("0011","装货工厂外排队");
                message_title.setVisibility(View.VISIBLE);
                message_deletails.setVisibility(View.VISIBLE);
                message_title.setText(signMode.getStatusName());
                message_deletails.setText("您已签到成功，请在厂外等候入场通知");
            }else if(signMode.getStatusCode().equals(StringUtil.READY_TO_LOAD)){
                //准备装货
                Log.e("0011","准备装货");

                message_title.setVisibility(View.VISIBLE);
                message_deletails.setVisibility(View.VISIBLE);
                message_deletails2.setVisibility(View.VISIBLE);
                bottom_bar_code.setVisibility(View.VISIBLE);

                message_title.setText(signMode.getStatusName());
                message_deletails.setText("您好，请您到装车位，感谢您的耐心等候！");
                message_deletails2.setText("请向装货员出示下方运单条码");
                bar_code_num.setText(signMode.getCode());
                creatAndSetBitmap(signMode.getCode());

            }else if(signMode.getStatusCode().equals(StringUtil.IN_LOADING)){
                //装货中
                Log.e("0011","装货中");
                message_title.setVisibility(View.VISIBLE);
                message_deletails.setVisibility(View.VISIBLE);
                message_deletails2.setVisibility(View.VISIBLE);
                message_deletails3.setVisibility(View.VISIBLE);
                prompt.setVisibility(View.VISIBLE);
                bottom_bar_code.setVisibility(View.VISIBLE);


                message_title.setText(signMode.getStatusName());
                message_deletails.setText("开始装货，您的装货员：");
                message_deletails2.setText("工号xxx xxx 装货中\n工号xxx xxx 装货中\n工号xxx xxx 装货中");
                message_deletails3.setText("合作愉快");
                prompt.setText("请在装货完成后再次出示下方条形码！");
                bar_code_num.setText(signMode.getCode());
                creatAndSetBitmap(signMode.getCode());

            }else if(signMode.getStatusCode().equals(StringUtil.LEAVE_THE_FACTORY)){
                //离场中（装货工厂）
                Log.e("0011","离场中（装货工厂）");
            }else if(signMode.getStatusCode().equals(StringUtil.LINE_UP_DESTINATION)){
                //收货工厂外排队
                Log.e("0011","收货工厂外排队");
            }else if(signMode.getStatusCode().equals(StringUtil.READY_TO_RECEIPT)){
                //准备收货
                Log.e("0011","准备收货");
            }else if(signMode.getStatusCode().equals(StringUtil.RECEIVING)){
                //收货中
                Log.e("0011", "收货中");

                message_title.setVisibility(View.VISIBLE);
                message_deletails.setVisibility(View.VISIBLE);
                message_deletails2.setVisibility(View.VISIBLE);
                message_deletails3.setVisibility(View.VISIBLE);
                prompt.setVisibility(View.VISIBLE);
                bottom_bar_code.setVisibility(View.VISIBLE);

                message_title.setText(signMode.getStatusName());
                message_deletails.setText("开始卸货，您的卸货员：");
                message_deletails2.setText("工号xxx xxx 卸货中\n工号xxx xxx 卸货中\n工号xxx xxx 卸货中");
                message_deletails3.setText("合作愉快");
                prompt.setText("请在卸货完成后再次出示下方条形码！");
                bar_code_num.setText(signMode.getCode());
                creatAndSetBitmap(signMode.getCode());
            }
        }
    }
    //创建条形码时需要知道控件宽高，在activity的oncreat方法内执行此方法时因为控件宽高未知，会出现不显示的情况，所以要启动线程延时执行任务
    private void creatAndSetBitmap(String num){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                    barCodeUtil = new BarCodeUtil(barCodeImg.getWidth(), barCodeImg.getHeight());
                    bitmap =  barCodeUtil.bitmap1(signMode.getCode());
                    httpAndUiHandle.sendEmptyMessage(MessageInfoUtil.SET_BITMAP);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
    private Handler httpAndUiHandle=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case MessageInfoUtil.SET_BITMAP:
                    barCodeImg.setImageBitmap(bitmap);
                    break;

            }

        }
    };

    /**
     * 隐藏所以控件
     */
    private void hideAllView(){
        message_title.setVisibility(View.INVISIBLE);
        message_deletails.setVisibility(View.INVISIBLE);
        message_deletails2.setVisibility(View.INVISIBLE);
        message_deletails3.setVisibility(View.INVISIBLE);
        prompt.setVisibility(View.INVISIBLE);
        bottom_bar_code.setVisibility(View.INVISIBLE);
    }

    private void initinitControl(){
        bottom_bar_code=(RelativeLayout)view.findViewById(R.id.bottom_bar_code);
        barCodeImg=(ImageView)view.findViewById(R.id.bar_code_img);
        bar_code_num=(TextView)view.findViewById(R.id.bar_code_num);
        message_title=(TextView)view.findViewById(R.id.message_title);
        message_deletails=(TextView)view.findViewById(R.id.message_deletails);
        message_deletails2=(TextView)view.findViewById(R.id.message_deletails2);
        message_deletails3=(TextView)view.findViewById(R.id.message_deletails3);
        prompt=(TextView)view.findViewById(R.id.prompt);
    }


    public void initHandle(Handler handler){
        this.handler=handler;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if(null!=bitmap){
            bitmap.recycle();
        }

    }
    public void initData(SignMode mode){
    this.signMode=mode;
    }

}
